package com.exampleyyy.demoyyy.common;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;

import com.exampleyyy.demoyyy.ShiTiYYY.Zhifubao;
import org.springframework.stereotype.Component;


@Component//注解 做成一个组件类
public class Alipay {

    public String pay(Zhifubao zhifubao) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        // 创建配置对象
        String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCjGe4ttgVfmunitfixlq2QBdofKpCugC0/tLQhpWqu/kEUiKPVy788k36RSoTV4Xmr873rxMDvze3cxCcr/C7vtTJz//RZ7j8+jDmrAlxdl9X65BCcJwpaqz9oMzO80hEBs2NQM1RFYs5iMzC+ze/haoFiIIFxPZB7o3mlcc6EbwOE8NsMxW9oY34klk5Rxaxx/BtxDKuC1CxS1k29iaNDZQUPRcG+sI0s8fH3oVjxNfc9JvBTngKNstXqUquqy6Yj7jkBoE4XXvUV4oQtJXF1Zd+1I2ae3bPHAPuVv4i5B7BTiI8Vh7BLVK2IFnCBPQibUQJ4jgxJDl3Pl+98xu4/AgMBAAECggEAD1FhICPnL0pyStuCRtN663rROHob6neZSFFlVX+enF6+p/oJQQwIotzLV0PjY97wOWkD3j+1SxBRFZnKgAQIOHURDvnr7gJFKE+tHtVy7UtUSmLe+HtmKovIwbWFV2d3N2ysmByp/nZAIdLi8hiPPeFt01RwM71vc10wEIy2s5M+0g53vod2UHQrbmJ7obsXqR0JUaKnNJ0wW5N3es/na592dg4SCMZblXoBZHUqo5p8J63s5NwNizCJ3XlcqGJYY6xIK+v2H4YE8R+oQQZhR7awE1bDHcR7UgMvdxEWrG+qTWj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em+sAkQk/u9JFdB29/fAYH0iD415+gZlMWlxvyv+yAqpHrNfc2VYE0QFWT0dw9D9+q7mZr6WyP/xaYd2HuDDG3DliM5SlRY5MaVD07/F2e1QvxOK8EMARwVUXhAysJmWwQcRZ5qFpRtSr1FPCIVO4E0hiu/Buq4GwKPBb5PXiS13KC6GN1QeRZO9cd4VBupXyE7Z5U4fVeIfSXnd8B3H0rGoZZcNEXQQjLMH3mEQ4w1CubiWSBZkpQIDAQAB"; // 自己的公钥
        String APPID = "9021000153675271"; // 自己的应用编号
        String returnUrl = "http://112.124.103.39:8081/front/page/pay-success.html";
        // 支付成功后跳转的页面
        String serverUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
        // 支付宝网关地址

        alipayConfig.setServerUrl(serverUrl);
        alipayConfig.setAppId(APPID);
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        // 设置这些数据

        alipayConfig.setFormat("json");// 设置格式
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");

        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        // 创建支付客户端对象
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        // 创建手机浏览器请求对象,用来请求打开支付宝app
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        // 手机浏览器支付 ,拉起支付宝app

        model.setOutTradeNo(zhifubao.getOut_trade_no());// 订单编号
        model.setTotalAmount(zhifubao.getTotal_amount()); // 订单金额
        model.setSubject(zhifubao.getSubject());// 订单主题
        model.setProductCode("QUICK_WAP_WAY"); // 产品编号,手机端浏览器支付,固定,不需要修改
        model.setSellerId("hpjdmi4570@sandbox.com"); // 设置成自己的商家支付宝账号
        // 注意:一定要用用商家的支付宝账号,在沙箱中的商家信息中
        request.setBizModel(model);//将数据设置到请求对象中
        request.setReturnUrl(returnUrl); // 设置支付成功之后跳转的页面

        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);
        // 发起支付请求
        String result = response.getBody();
        // 返回支付后的响应结果
        return result;
    }
}